import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { CommonService } from '../common/common.service';
import { ConfirmDialogService } from '../common/confirm-dialog/confirm-dialog.service';
import { CommonDetail, DetailButton } from '../common/model/commom.model';
import { ASYNC_URLS } from '../r-mobile-url';

@Component({
  selector: 'oy-manager-requisition-detail',
  templateUrl: './manager-requisition-detail.component.html',
  styleUrls: ['./manager-requisition-detail.component.scss']
})
/**
 * 描述：请购详情/审核TS，根据路由type区分audit（审核），detail(详情)
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 作者：oyjl
 * 时间：2020-04-27 14:04:34
 */
export class ManagerRequisitionDetailComponent implements OnInit {

  public commonDetail: CommonDetail;
  public id: string; // 订单ID
  constructor(
    public commonService: CommonService,
    public activatedRoute: ActivatedRoute,
    public confirmDialogService: ConfirmDialogService) {
    this.activatedRoute.queryParams.subscribe(data => {
      this.id = data.id;
    });
  }

  ngOnInit() {
    const type = this.activatedRoute.snapshot.params.type;
    const buttons: Array<DetailButton> = [
      {
        text: '通过审核', dialog: {
          head: '确定通过请购订单 ${requestNo}', text: '通过理由', required: false,
          callback: ((data, $this) => {
            this.commonService.sendPost(ASYNC_URLS.requisitionUpdate, {
              oaRequisition: {
                requisitionId: this.id,
                status: '2',
                reserve: data
              },
              dingFlag: '1'
            }).subscribe(res => {
              if (res.operateSuccess) {
                this.confirmDialogService.confirm({
                  type: 'success',
                  headText: '请购单审核',
                  contentText: res.msg || '审核成功通过！'
                });
              } else {
                this.confirmDialogService.confirm({
                  type: 'error',
                  headText: '请购单审核',
                  contentText: res.msg || '审核失败！'
                });
              }
              $this.closeDialog();
            });
          })
        }
      },
      {
        text: '拒绝申请', dialog: {
          head: '确定拒绝请购订单 ${requestNo}', text: '拒绝理由', required: true,
          callback: ((data, $this) => {
            this.commonService.sendPost(ASYNC_URLS.requisitionUpdate, {
              oaRequisition: {
                requisitionId: this.id,
                status: '3',
                reserve: data
              },
              dingFlag: '1'
            }).subscribe(res => {
              if (res.operateSuccess) {
                this.confirmDialogService.confirm({
                  type: 'success',
                  headText: '请购单审核',
                  contentText: res.msg || '审核成功拒绝！'
                });
              } else {
                this.confirmDialogService.confirm({
                  type: 'error',
                  headText: '请购单审核',
                  contentText: res.msg || '拒绝失败！'
                });
              }
              $this.closeDialog();
            });
          })
        }
      }
    ];
    this.commonDetail = new CommonDetail({
      id: 'requisitionId',
      head: type === 'audit' ? '请购单审核' : '请购单详情',
      url: ASYNC_URLS.requisitionDetail,
      req: {
        oaRequisition: {},
        dingFlag: '1'
      },
      resKey: 'oaRequisition',
      moduleHead: { text: '请购单号', orderNum: 'requestNo', status: 'requestStatusName', dict: '' },
      modules: [
        {
          head: '请购信息', isRequest: false, type: 'text',
          req: {},
          fields: [
            { text: '项目名称', field: 'oaProject.projectName' },
            { text: '请购原因', field: 'requestReason' },
            { text: '请购人', field: 'personName' },
          ]
        },
        {
          head: '请购明细', isRequest: false, type: 'table',
          req: {},
          jumpText: '请购详情',
          isIndex: true,
          resKey: 'oaRequisitionMaterials',
          jumpUrl: '/mobile/requisitionDetail/detail',
          fields: [
            { text: '物料编码', field: 'materialName' },
            { text: '型号规格', field: 'spec' },
            { text: '请购数量', field: 'requestCount' },
            { text: '参考单价', field: 'referencePrice', dictKey: 'currency' }
          ],
          headTotal: [
            { text: '合计', fields: ['requestCount', 'referencePrice'], style: { color: 'red' }, dictKey: 'currency' },
            { text: '数量', fields: ['requestCount'] }
          ]
        }
      ]
    });
    if (type === 'audit') {
      this.commonDetail.detail.buttons = buttons;
    }
  }
}
